//
//  CollectAticleTableview.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/23.
//  Copyright © 2016年 game. All rights reserved.
//

#import "CollectAticleTableview.h"
#import "MyCollectCellTableViewCell.h"
#import "CollectSelectModel.h"
#import "SeleCtContrl.h"
#import "SpacePagerModel.h"
#import "ArticleCollectModel.h"
#import "VideoDescrib.h"
#import "NewestAticleCtr.h"
#import "RootTableview.h"
#import "MJRefresh.h"
@interface CollectAticleTableview ()<UITableViewDataSource,UITableViewDelegate,MyCollectCellTableViewCellDeleget>
@property (nonatomic,strong) NSMutableArray *dataAry;

@property (nonatomic,assign) CGFloat CellHeight;

@property (nonatomic,assign) NSInteger nextpage;

@property (nonatomic,strong) CollectSelectModel *model;
@property (nonatomic,strong) AppDelegate *app;
@property (nonatomic,strong) NSMutableDictionary *cellHightDic;
@end

@implementation CollectAticleTableview

-(NSMutableDictionary *)cellHightDic{
    
    if (!_cellHightDic) {
        _cellHightDic = [NSMutableDictionary dictionary];
    }
    return _cellHightDic;
}


-(NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    
    if (self = [super initWithFrame:frame style:style]) {
        
        self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        self.dataSource = self;
        
        self.delegate = self;
        
        [self registerNib:[UINib nibWithNibName:@"MyCollectCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        self.separatorStyle = 0;
        self.backgroundColor = tabGropBackColor;
        
        
        self.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(RefreshHead)];
        
        self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(RefreshFooter)];
        
        
        [self getDataWhithPage:1];
        
        self.nextpage = 2;
        
        __weak typeof(self) weakSelf = self;
        self.TapBlok = ^{
            
            [weakSelf RefreshHead];
        };

        self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    }
    return self;
}
-(void)RefreshFooter{
    
    SpacePagerModel *pageModel  = self.model.pager;
    if (self.nextpage >pageModel.total_page.integerValue) {
        [self.footer noticeNoMoreData];
    }else
        [self getDataWhithPage:self.nextpage];
    self.nextpage ++;
    
}
-(void)RefreshHead{
    [self.dataAry removeAllObjects];
    [self getDataWhithPage:1];
    self.nextpage = 2;
    
}

-(void)getDataWhithPage:(NSInteger )page{
    
    NSString *url= [NSString stringWithFormat:@"http://school.diaoyur.cn/article/user-collection?uid=%@",self.app.userModel.uid];
    [[Manager sharedManager]RequestWhithURL:url AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {

        
        if (error) {
            self.isShowFile = YES;
            [self reloadData];
        }else{
            self.model = [[CollectSelectModel alloc]initWithDictionary:responseObject[@"data"][@"collection"] error:nil];
            
            [self.dataAry addObjectsFromArray:responseObject[@"data"][@"collection"] ];
            [self.header endRefreshing];
            [self.footer endRefreshing];
            
            self.isShowImage = YES;
            [self reloadData];
        }
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataAry.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return  [[self.cellHightDic valueForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]] floatValue]+10;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCollectCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (self.dataAry.count>0) {
        
        cell.articleModel = [[ArticleCollectModel alloc]initWithDictionary:self.dataAry[indexPath.row] error:nil];
         [self.cellHightDic setValue:[NSString stringWithFormat:@"%f",cell.height-10] forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
    }
    
    cell.deleget = self;
    return cell;
}



/**
 *  cell 取消收藏代理方法
 */
-(void)CellCancelClick:(UITableViewCell *)cell{
    MyCollectCellTableViewCell *collectCell = (MyCollectCellTableViewCell *)cell;
    NSIndexPath *indexPath = [self indexPathForCell:cell];
    
    NSDictionary *dic = @{@"uid":self.app.userModel.uid,@"article_id":collectCell.tid};
  
    
    [[Manager sharedManager]ResponseWhithURL:@"http://school.diaoyur.cn/article/cancel-collection" AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
    
    
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            
            if (indexPath.row<self.dataAry.count) {
                [self.dataAry removeObjectAtIndex:indexPath.row];//移除数据源的数据
                
                
                [self deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
                
                if ((indexPath.row+1)>=self.dataAry.count) {
                    [self getDataWhithPage:self.nextpage];
                }
            }
    
    }
}];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyCollectCellTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (cell.skin == 1){
        VideoDescrib *video = [VideoDescrib new];
        video.AticleID = cell.tid;
        [self.fatherView.navigationController  pushViewController:video animated:YES];
    }else{
        NewestAticleCtr *aticle = [NewestAticleCtr new];
        aticle.AticleID = cell.tid;
        [self.fatherView.navigationController pushViewController:aticle animated:YES];
    }

}

@end
